<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Users;
use fast\Util;
use think\Db;

class Pub extends Api {

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize() {
        
    }

    public function index() {
        $this->success('1234');
    }

    //用户登录
    public function login() {
        $openid = $this->request->param("wx_openid");
        $wx_headimgurl = $this->request->param('wx_headimgurl');
        $wx_nickname = $this->request->param('wx_nickname');

        $userM = new Users();
        $where = [];
        if ($openid == 'undefined') {
            $openid = '';
        }
        empty($openid) && $this->error("请传入微信openid");

        $where['wx_open_id'] = $openid;

        $userInfo = db('Users')->where($where)->find();

        if (empty($userInfo)) {
            $newuser['wx_open_id'] = $openid;
            $newuser['face'] = $wx_headimgurl;
            $newuser['wx_nick_name'] = $wx_nickname;

            $userM->data($newuser)->save();
        }


        //$objUser = M('Users');
        $userInfo = db('Users')->where($where)->find();
        empty($userInfo) && $this->error('登录失败');

        // $this->getUserStatus($userInfo['user_id']);
//        session('user_id', $userInfo['user_id']);
//        $date = strtotime($userInfo['create_time']);
//        $userInfo['create_time'] = date('Y-m-d', $date);


        $content = Util::randStr(60, 2) . $openid . time() . '123456';
        $userToken = Util::encrypt($content);

        $data['user_token'] = $userToken;

        if (!empty($wx_headimgurl)) {
            $data['face'] = $wx_headimgurl;
        }

        if (!empty($wx_nickname)) {
            $data['wx_nick_name'] = $wx_nickname;
        }

        $userM->save($data, ['user_id' => $userInfo['user_id']]);

        //返回phpsessid
//        $userInfo['PHPSESSID'] = session_id();
        /*         * ******token 查询返回********************************************** */
//        $arrUserWhere['user_id'] = $userInfo['user_id'];
        $userInfo['user_token'] = $userToken;

        unset($userInfo['user_id']);
        /*         * ******token 查询返回********************************************** */
        $this->success("登录成功", $userInfo);
    }

    public function getOpenId() {
        $appid = 'wx30307ffd212d849e';
        $appSec = '789fa6b70524a809bcc19ed6a206dc15';
        $code = $this->request->param('code');

        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $appSec . '&js_code=' . $code . '&grant_type=authorization_code';

        $info = file_get_contents($url); //发送HTTPs请求并获取返回的数据，推荐使用curl  
        $json = json_decode($info); //对json数据解码  
        $arr = get_object_vars($json);
        $openid = $arr['openid'];

        $this->success('获取成功', $arr);
    }

    public function getCarInfo() {
        $id = $this->request->param('id', 0, 257);

        if ($id == 0) {
            $this->error('错误的ID');
        }



        $car_info = DB::table('fa_car')
                ->alias('c')
                ->field('u.*,c.car_id,c.car_number,c.car_str')
                ->join('__USERS__ u ', ' c.user_id = u.user_id')
                ->where(['c.car_id' => $id])
                ->find();
//        
//        $car_info = M('Car')
//                ->table('ot_car c')
//                ->field('u.*,c.car_id,c.car_number,c.msg,c.car_str')
//                ->join('ot_users u on c.user_id = u.user_id')
//                ->where(['c.car_id' => $id])
//                ->find();


        if ($car_info) {

            $car_info['bri'] = '';
            if (!empty($car_info['brithday'])) {
                $bri = strtotime($car_info['brithday']);

                $car_info['bri'] = date('m-d', $bri);
            }

            $this->success('获取成功', $car_info);
        } else {

            $this->error('没有找到数据');
        }
    }
    
    public function getAdUrl() {
        $id = $this->request->param('id', 0);
        $data = db('Ad')->where(['ad_id' => $id])->find();

        $user_token = $this->request->param("user_token");
        if ($user_token) {
            //查询库
            $where['user_token'] = $user_token;
            $ut = db('Users')->where($where)->find();

            $log['user_id'] = $ut['user_id'];
            $log['ad_id'] = $id;
            $log['ad_position'] = $data['ad_position'];
            $log['createtime'] = time();
            $log['updatetime'] = time();

            Db::table('fa_ad_log')->insert($log);
        }
        $res = [];
        if ($data['ad_type'] == 1) {
            $res['url'] = 'https://lc.feiyouce.com/Index/index/ad/id/' . $id;
        } else {
            $res['url'] = $data['h5_url'];
        }
        
         $this->success('获取成功', $res);
    }

}
